<template>
  <div>
    <el-form ref="ruleForm" style="margin-top: 30px;" :model="user" label-width="100px">
      <el-form-item label="账号" prop="username">
        <el-input v-model="user.username" disabled />
      </el-form-item>
      <el-form-item label="密码" prop="username">
        <el-input v-model="user.password" type="password" :disabled="judgeDisabled" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="user.name" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="user.nickname" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="user.sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="头像" prop="header">
        <el-upload
          v-loading="imgLoading"
          element-loading-text="图片上传中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="#fff"
          element-
          class="avatar-uploader"
          :action="uploadUrl"
          :headers="headers"
          :show-file-list="false"
          :on-success="handleUploadSuccess"
          :before-upload="imageUploading"
        >
          <img v-if="user.header" :src="user.header" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item label="邮箱" prop="userEmail">
        <el-input v-model="user.userEmail" />
      </el-form-item>
      <el-form-item label-width="100px">
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import userApi from '@/api/myuser'
export default {
  name: 'UserUpdate',
  props: {
    user: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      headers: {
        Authorization: getToken()
      },
      imageUrl: '',
      uploadUrl: process.env.VUE_APP_UPLOAD_URL,
      dateValue: new Date(),
      imgLoading: false,
      fileList: [
      ],
      fileLoading: false
    }
  },
  computed: {
    judgeDisabled() {
      return !!this.user.openType
    }
  },
  created() {
  },
  methods: {
    submitForm() {
      const loading = this.$loading({
        lock: true,
        text: '跟新中请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      userApi.update(this.user).then(res => {
        if (res.code === 200) {
          this.music = res.data
          loading.close()
          this.$message.success(res.msg)
          this.$emit('closeUserUpdateDialog')
          this.$emit('getUserList')
        } else {
          this.$message.success(res.msg)
          loading.close()
        }
      }).catch(err => {
        console.log(err)
        loading.close()
      })
    },
    handleUploadSuccess(res, file) {
      this.user.header = res.data
      this.imgLoading = false
    },
    imageUploading() {
      this.imgLoading = true
    }
  }
}
</script>

<style scoped>
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    background-size: cover;
    display: block;
  }
</style>

